package com.loong.nnm.utils;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import net.juniper.netconf.Device;
import net.juniper.netconf.NetconfException;
import net.juniper.netconf.XML;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.List;

public class NetconfServer extends Device {
    public NetconfServer(JSch sshClient, String hostName, Integer port, Integer timeout, Integer connectionTimeout, Integer commandTimeout, String userName, String password, Boolean keyBasedAuthentication, String pemKeyFile, Boolean strictHostKeyChecking, String hostKeysFileName, List<String> netconfCapabilities) throws NetconfException {
        super(sshClient, hostName, port, timeout, connectionTimeout, commandTimeout, userName, password, keyBasedAuthentication, pemKeyFile, strictHostKeyChecking, hostKeysFileName, netconfCapabilities);
    }

    private Session session;

    @Override
    public Session getSshSession() {
        return super.getSshSession();
    }

    public void initSession(){
        Session sshSession = getSshSession();
        try {
            sshSession.connect();
            if (sshSession.isConnected()) {
                session = sshSession;
            }
        } catch (JSchException e) {
            e.printStackTrace();
        }
    }


    @Override
    public XML executeRPC(String rpcContent) throws SAXException, IOException {
        return super.executeRPC(rpcContent);
    }

    public void getMe(){
        try {
            session.sendKeepAliveMsg();
            getSshSession().connect();
            executeRPC("<get>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
